package user.zc.action;

import user.zc.action.comp.SaveOrUpdateAction;
import user.zc.controller.ISystemConfig;
import user.zc.vo.ColumnDTO;

import javax.swing.*;
import java.util.Map;
import java.util.function.Consumer;

public class AddOrUpdateSystemDefaultColumnAction extends SaveOrUpdateAction<ColumnDTO,Boolean> {

    public AddOrUpdateSystemDefaultColumnAction(ISystemConfig systemConfigController, Consumer<Boolean> callback){
        this(systemConfigController,null,callback);
    }
    public AddOrUpdateSystemDefaultColumnAction(ISystemConfig systemConfigController,ColumnDTO columnDTO, Consumer<Boolean> callback){
        super(callback,columnDTO,500,6*SCALE);
        try {
            addItem("名称","key",true);
            addItem("类型","type",true,SaveOrUpdateAction.InputType.ColumnType);
            addItem("备注","comment",false);
            addItem("长度","len",false,"0");
            addItem("精度","scale",false,"0");
            addItem("主键","isPk",true,SaveOrUpdateAction.InputType.BooleanType);
            openDialog(columnDTO!=null?("修改系统默认列【"+columnDTO.getKey()+"】"):"新增系统默认列",(dto)->{
                try {
                    systemConfigController.saveOrUpdate(dto);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                JOptionPane.showMessageDialog(null,"保存成功");
                call(true);
            });
        } catch (Exception e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,
                    e.getMessage(), "错误提示", JOptionPane.ERROR_MESSAGE);
        }
    }


    @Override
    protected ColumnDTO map2Entity(Map<String, String> map) {
        return new ColumnDTO(map.get("key"),map.get("type"),map.get("comment"),Integer.parseInt(map.get("len")),Integer.parseInt(map.get("scale")),Boolean.parseBoolean(map.get("isPk")));
    }
}
